"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : dbcompatibility-whale
Case Name   : 函数gms_output.new_line()向缓冲区中置入新的行
Create At   : 2023/12/18
Owner       : @zou_jialiang050
Description :
    1.函数gms_output.new_line()与函数结合
Expect      :
    1.成功
History     :
"""
import os
import unittest
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class Whale(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f"-----{os.path.basename(__file__)} start-----")
        self.constant = Constant()
        self.common = Common()
        self.sh_primy = CommonSH('PrimaryDbUser')
        self.dbname = 'db_whale_39'
        self.table = 'tb_whale_39'
        self.function = 'fun_whale_39'
    
    def test_gms_output(self):
        text = "---pre1:创建A兼容性数据库;expect:成功---"
        self.logger.info(text)
        pre_cmd1 = f"drop database if exists {self.dbname};" \
            f"create database {self.dbname} dbcompatibility='A';"
        pre_msg1 = self.sh_primy.execut_db_sql(pre_cmd1)
        self.logger.info(pre_msg1)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, pre_msg1,
                      text + '执行失败')
        text = "---pre2:在A数据库中创建whale插件;expect:成功---"
        self.logger.info(text)
        pre_cmd2 = f"create extension whale;"
        pre_msg2 = self.sh_primy.execut_db_sql(pre_cmd2, dbname=self.dbname)
        self.logger.info(pre_msg2)
        self.assertIn(self.constant.extension_create_success, pre_msg2,
                      text + '执行失败')

        text = "---step1:结合函数使用;expect:成功---"
        self.logger.info(text)
        sql_cmd1_1 = f"CREATE TABLE {self.table} " \
            f"(buff VARCHAR(20), status INTEGER);"
        msg1_1 = self.sh_primy.execut_db_sql(sql_cmd1_1, dbname=self.dbname)
        self.logger.info(msg1_1)
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS, msg1_1,
                      text + '执行失败')

        sql_cmd1_2 = f'''
        CREATE FUNCTION {self.function}() RETURNS VOID AS \$\$
            DECLARE
                buff1 VARCHAR(20);
                stts INTEGER := 10;
            BEGIN
                PERFORM GMS_OUTPUT.DISABLE();
                PERFORM GMS_OUTPUT.ENABLE();
                PERFORM GMS_OUTPUT.SERVEROUTPUT ('f');
                PERFORM GMS_OUTPUT.ENABLE(2000);
                FOR j IN 1..10 LOOP
                    PERFORM GMS_OUTPUT.PUT ('A');
                END LOOP;
                PERFORM GMS_OUTPUT.NEW_LINE();
                SELECT INTO buff1,stts lines[1],numlines FROM
                GMS_OUTPUT.GET_LINES(stts);
                INSERT INTO {self.table} VALUES (buff1, stts);
                PERFORM GMS_OUTPUT.DISABLE();
                PERFORM GMS_OUTPUT.ENABLE();
            END;
            \$\$ LANGUAGE plpgsql;'''
        msg1_2 = self.sh_primy.execut_db_sql(sql_cmd1_2, dbname=self.dbname)
        self.logger.info(msg1_2)
        self.assertIn(self.constant.CREATE_FUNCTION_SUCCESS_MSG, msg1_2,
                      text + '执行失败')

        sql_cmd1_3 = f"SELECT {self.function};"
        msg1_3 = self.sh_primy.execut_db_sql(sql_cmd1_3, dbname=self.dbname)
        self.logger.info(msg1_3)
        self.common.equal_sql_mdg(msg1_3, f'{self.function}', '', '(1 row)',
                                  flag='1')

        sql_cmd1_4 = f"SELECT buff FROM {self.table};"
        msg1_4 = self.sh_primy.execut_db_sql(sql_cmd1_4, dbname=self.dbname)
        self.logger.info(msg1_4)
        self.common.equal_sql_mdg(msg1_4, 'buff', 'AAAAAAAAAA', '(1 row)',
                                  flag='1')
    
    def tearDown(self):
        text = '----清理环境----'
        self.logger.info(text)
        cl_cmd_1 = f"drop table {self.table};"
        cl_msg_1 = self.sh_primy.execut_db_sql(cl_cmd_1, dbname=self.dbname)
        self.logger.info(cl_msg_1)
        self.assertIn(self.constant.DROP_TABLE_SUCCESS, cl_msg_1,
                      text + '执行失败')

        cl_cmd_2 = f"drop function {self.function};"
        cl_msg_2 = self.sh_primy.execut_db_sql(cl_cmd_2, dbname=self.dbname)
        self.logger.info(cl_msg_2)
        self.assertIn(self.constant.DROP_FUNCTION_SUCCESS_MSG, cl_msg_2,
                      text + '执行失败')
        
        cl_cmd3 = f"drop database {self.dbname};"
        cl_msg_3 = self.sh_primy.execut_db_sql(cl_cmd3)
        self.logger.info(cl_msg_3)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, cl_msg_3,
                      text + '执行失败')
        self.logger.info(f'-----{os.path.basename(__file__)} end-----')
